#include <iostream>
#include <algorithm>
#include <cstring>
#define endl '\n'
using namespace std;
const int N = 2020;

int a[N][N];
int pre[N][N];

int main(){
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int n;
	cin >> n;
	while(n--){
		int x, y, w;
		cin >> x >> y >> w;
		x++, y++;
		a[x][y] += w;
	}
	for(int i = 1; i <= 2001; i++){
		for(int j = 1; j <= 2001; j++){
			pre[i][j] = pre[i - 1][j] + pre[i][j - 1] + a[i][j] - pre[i - 1][j - 1];
		}
	}
	int q;
	cin >> q;
	while(q--){
		int x1, y1, x2, y2;
		cin >> x1 >> y1 >> x2 >> y2;
		x1++, y1++, x2++, y2++;
		cout << pre[x2][y2] - pre[x2][y1 - 1] - pre[x1 - 1][y2] + pre[x1 - 1][y1 - 1] << endl;
	}
	return 0;
}